package com.smart.manager.weixin.mp.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.ArcoTips;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.core.Page;
import com.smart.manager.weixin.mp.entities.bodys.RWeixinMpTags;
import com.smart.manager.weixin.mp.entities.models.WeixinMpTags;
import com.smart.manager.weixin.mp.repository.WxmpTagsRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;


/**
 * @author Administrator
 */

@Api(tags = "分组标签")
@RestController
@RequestMapping(SmartConstant.MANAGER_API_PREFIX + "weixin/mp/tags")
@RequiredArgsConstructor
public class WxmpTagsApi {

    private final WxmpTagsRepository tagsRepository;


    @GetMapping(value = "pageList")
    @ApiOperation(value = "查询标签")
    @SaCheckPermission("wxmp:tags:pageList")
    public AjaxResult<Page<WeixinMpTags>> pageList(@Validated RWeixinMpTags.SearchBody body) {
        Page<WeixinMpTags> result = tagsRepository.pageList(body);
        return AjaxResult.data(result);
    }


    @PostMapping(value = "sync/{appId}")
    @ApiOperation(value = "新增标签")
    @SaCheckPermission("wxmp:tags:sync")
    public AjaxResult<String> sync(@PathVariable String appId) {
        tagsRepository.syncTags(appId);
        return AjaxResult.success();
    }


    @PostMapping(value = "add")
    @ApiOperation(value = "新增标签")
    @SaCheckPermission("wxmp:tags:add")
    public AjaxResult<String> add(@RequestBody @Validated RWeixinMpTags.EditBody body) {
        tagsRepository.add(body);
        return AjaxResult.success();
    }


    @PutMapping(value = "update/{id}")
    @ApiOperation(value = "修改标签")
    @SaCheckPermission("wxmp:tags:update")
    public AjaxResult<String> update(@PathVariable Long id, @RequestBody @Validated RWeixinMpTags.EditBody body) {
        tagsRepository.update(id, body);
        return AjaxResult.success();
    }


    @DeleteMapping(value = "delete")
    @ApiOperation(value = "删除标签")
    @SaCheckPermission("wxmp:tags:delete")
    public AjaxResult<String> delete(@RequestBody @NotEmpty(message = ArcoTips.PK_EMPTY) List<Long> ids) {
        tagsRepository.delByIds(ids);
        return AjaxResult.success();
    }

}
